package com.sprouting.ops.controller;

import cn.hutool.core.lang.Validator;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsIp;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.service.OpsIpService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ip仓库
 * 暂不使用
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/11/8 18:17
 */
@RestController
@RequestMapping("ip")
public class OpsIpController {

    @Resource
    private OpsIpService opsIpService;


    /**
     * 获取机器的Ip信息，不分页。这数据不会很多，直接查询出来
     * @param computerServerId 机器ip
     * @return
     */
    @PostMapping("getByComputerServerId")
    public ResultJson getByComputerServerId(String computerServerId){
        if (CheckTool.checkNull(computerServerId)){
            return ResultJson.errorCheck();
        }
        return ResultJson.success(opsIpService.getByComputerServerId(computerServerId));
    }

    /**
     * 保存一个Ip
     * @param opsIp ip信息
     * @return
     */
    @IdempotentSingle
    @PostMapping("saveIp")
    public ResultJson saveIp(@Validated(AddGroup.class) OpsIp opsIp){
        String msg = checkOpsIp(opsIp);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsIp.setId(IdTool.getId());
        EntityTool.saveBase(opsIp);
        opsIpService.save(opsIp);
        return ResultJson.success(opsIp);
    }

    /**
     * 更新一个Ip
     * @param opsIp ip信息
     * @return
     */
    @IdempotentSingle
    @PostMapping("updIp")
    public ResultJson updIp(@Validated(UpdGroup.class) OpsIp opsIp){
        String msg = checkOpsIp(opsIp);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.updBase(opsIp);
        opsIpService.updateById(opsIp);
        return ResultJson.success(opsIp);
    }

    /**
     * 物理删除ip
     * @param ipId ip的id
     * @return
     */
    @IdempotentSingle
    @PostMapping("delSite")
    public ResultJson delSite(String ipId){
        if (CheckTool.checkNull(ipId)){
            return ResultJson.errorCheck();
        }

        // 无保留价值，直接删除
        opsIpService.removeById(ipId);
        return ResultJson.success();
    }

    /**
     * 检查ip
     * @param opsIp ip信息
     * @return
     */
    private String checkOpsIp(OpsIp opsIp){
        OpsIp ipDb = opsIpService.getByIpFilter(opsIp.getId(), opsIp.getComputerServerId(), opsIp.getIp());
        if (ipDb != null){
            return "ip已使用，不需要重复添加";
        }

        OpsIp mainDb = opsIpService.getByMainFilterId(opsIp.getComputerServerId(), opsIp.getId());
        if (mainDb != null){
            return "一台主机/服务器/计算机 只能有一个主ip";
        }

        if (opsIp.getMain() == null){
            opsIp.setMain(ConstantBase.YesOrNo.NO);
        }
        if (opsIp.getType() == null){
            opsIp.setType(ConstantBase.IpInternal.INTERNAL);
        }
        if (!Validator.isIpv4(opsIp.getIp())){
            return "无效的IP地址";
        }

        return null;
    }
}
